package com.ruoyi.lowcode.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.constant.CacheNames;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StreamUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.redis.CacheUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.ruoyi.lowcode.domain.bo.LcDatabaseSourceBo;
import com.ruoyi.lowcode.domain.vo.LcDatabaseSourceVo;
import com.ruoyi.lowcode.domain.LcDatabaseSource;
import com.ruoyi.lowcode.mapper.LcDatabaseSourceMapper;
import com.ruoyi.lowcode.service.ILcDatabaseSourceService;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 数据源列Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-30
 */
@RequiredArgsConstructor
@Service
public class LcDatabaseSourceServiceImpl implements ILcDatabaseSourceService {

    private final LcDatabaseSourceMapper baseMapper;

    /**
     * 查询数据源列
     */
    @Override
    public LcDatabaseSourceVo queryById(Long databaseSourceId){
        return baseMapper.selectVoById(databaseSourceId);
    }

    /**
     * 查询数据源列列表
     */
    @Override
    public TableDataInfo<LcDatabaseSourceVo> queryPageList(LcDatabaseSourceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LcDatabaseSource> lqw = buildQueryWrapper(bo);
        Page<LcDatabaseSourceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询数据源列列表
     */
    @Override
    public List<LcDatabaseSourceVo> queryList(LcDatabaseSourceBo bo) {
        LambdaQueryWrapper<LcDatabaseSource> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LcDatabaseSource> buildQueryWrapper(LcDatabaseSourceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LcDatabaseSource> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDatabaseSourceName()), LcDatabaseSource::getDatabaseSourceName, bo.getDatabaseSourceName());
        lqw.eq(StringUtils.isNotBlank(bo.getIp()), LcDatabaseSource::getIp, bo.getIp());
        lqw.eq(StringUtils.isNotBlank(bo.getPort()), LcDatabaseSource::getPort, bo.getPort());
        lqw.eq(StringUtils.isNotBlank(bo.getUsername()), LcDatabaseSource::getUsername, bo.getUsername());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), LcDatabaseSource::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getAttribute()), LcDatabaseSource::getAttribute, bo.getAttribute());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), LcDatabaseSource::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增数据源列
     */
    @Override
    public Boolean insertByBo(LcDatabaseSourceBo bo) {
        LcDatabaseSource add = BeanUtil.toBean(bo, LcDatabaseSource.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDatabaseSourceId(add.getDatabaseSourceId());
        }
        return flag;
    }

    /**
     * 修改数据源列
     */
    @Override
    public Boolean updateByBo(LcDatabaseSourceBo bo) {
        LcDatabaseSource update = BeanUtil.toBean(bo, LcDatabaseSource.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LcDatabaseSource entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除数据源列
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void loadingDatabaseSourceCache() {
        List<LcDatabaseSourceVo> databaseSourceVoList = baseMapper.selectVoList(
            new LambdaQueryWrapper<LcDatabaseSource>().eq(LcDatabaseSource::getStatus, UserConstants.DATABASE_SOURCE_NORMAL)
        );

        CacheUtils.put(CacheNames.LOW_CODE, "lowcode_database_source", databaseSourceVoList);
    }

    @Cacheable(cacheNames = CacheNames.LOW_CODE, key = "'lowcode_database_source'")
    @Override
    public List<LcDatabaseSourceVo> selectDictDataByType() {
        List<LcDatabaseSourceVo> databaseSourceVoList = baseMapper.selectVoList(
            new LambdaQueryWrapper<LcDatabaseSource>().eq(LcDatabaseSource::getStatus, UserConstants.DATABASE_SOURCE_NORMAL)
        );
        if (CollUtil.isNotEmpty(databaseSourceVoList)) {
            return databaseSourceVoList;
        }
        return null;
    }

}
